package com.zl.zsk.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//

import java.text.DateFormat;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lxb
 * @version 1.0.0
 * @desc:
 * @date 2020-03-04 16:40
 */
@Configuration
@EnableCaching
public class RedisConfig {

    @Autowired
    private RedisClusterConfigProperties redisClusterConfigProperties;

//    @Autowired
//    private RedisConnectionFactory factory;
//
//    @Override
//    @Bean
//    public CacheManager cacheManager(RedisClusterConfiguration cluster) {
////        RedisCacheManager redisCacheManager = RedisCacheManager.create(factory);
////        return redisCacheManager;
//        CacheManager cacheManager = RedisCacheManager.create(jedisConnectionFactory(cluster));
//        return cacheManager;
//    }
//
//    @Bean
//    public RedisTemplate<String, ?> redisTemplate(RedisConnectionFactory factory) {
//        RedisTemplate<String, ?> redisTemplate = new RedisTemplate();
//        RedisSerializer redisSerializer =new StringRedisSerializer();
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        //设置redis key不乱码
//        redisTemplate.setKeySerializer(redisSerializer);
//        //设置redis value不乱码
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//
//        redisTemplate.setHashKeySerializer(redisSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setConnectionFactory(factory);
//        return redisTemplate;
//    }

    @Bean
    public RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration rcc = new RedisClusterConfiguration(redisClusterConfigProperties.getNodes());
        rcc.setMaxRedirects(redisClusterConfigProperties.getMaxAttempts());
        rcc.setPassword(RedisPassword.of(redisClusterConfigProperties.getPassword()));
        return rcc;
    }
//
//    @Bean
//    public JedisCluster getJedisCluster() {
//        JedisPoolConfig poolConfig = new JedisPoolConfig();
//        // 截取集群节点
//        List<String> list = redisClusterConfigProperties.getNodes();
//        // 创建set集合
//        Set<HostAndPort> nodes = new HashSet<>();
//        // 循环数组把集群节点添加到set集合中
//        for (String node : list) {
//            String[] host = node.split(":");
//            //添加集群节点
//            nodes.add(new HostAndPort(host[0], Integer.parseInt(host[1])));
//        }
//        return new JedisCluster(nodes, redisClusterConfigProperties.getConnectionTimeout(), redisClusterConfigProperties.getSoTimeout(), redisClusterConfigProperties.getMaxAttempts(), redisClusterConfigProperties.getPassword(), poolConfig);
//    }
//
//    @Bean
//    public JedisConnectionFactory jedisConnectionFactory(RedisClusterConfiguration cluster) {
//        return new JedisConnectionFactory(cluster);
//    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        JavaTimeModule timeModule = new JavaTimeModule();
        timeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializerConfig());
        om.registerModule(timeModule);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(lettuceConnectionFactory());
        template.afterPropertiesSet();
        template.setKeySerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public RedisConnectionFactory lettuceConnectionFactory(){
        return new LettuceConnectionFactory(redisClusterConfiguration());
    }
}